﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tool
{
    public class Count
    {

        #region 生产数据统计
        public static string PathData = AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "Count.ini";//数据统计路径

        //白班
        public static string[] DayTime = new string[] { "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };

        //夜班
        public static string[] NightTime = new string[] { "21", "22", "23", "0", "1", "2", "3", "4", "5", "6", "7", "8" };

        //定义一个委托

        public delegate void CountRefreshedEventHandler();

        //定义一个事件

        public static event CountRefreshedEventHandler CountRefreshedevent;
        public delegate void OKNGEventHandler(int s, string Result);

        //定义一个事件

        public static event OKNGEventHandler OKNGRefreshedevent;


        //定义24小时 总量 OK数量 NG数量

        public static long[] TotalArray = new long[24];//24小时总量

        public static long[] OKArray = new long[24];//24小时OK量

        public static long[] NGArray = new long[24];//24小时NG量

        public static long Total1 { get; set; }

        public static long OK1 { get; set; }

        public static long NG1 { get; set; }
        public static int Time { get; set; }

        public static string OKPercent1()
        {
            double d;
            if (OK1 != 0 && Total1 != 0)
            {
                d = Convert.ToDouble(OK1) / Convert.ToDouble(Total1) * 100;
                return d.ToString("f2") + "%";
            }
            else
            {
                return "0%";
            }
        }

        public static string NGPercent1()
        {
            double d;
            if (NG1 != 0 && Total1 != 0)
            {
                d = Convert.ToDouble(NG1) / Convert.ToDouble(Total1) * 100;
                return d.ToString("f2") + "%";
            }
            else
            {
                return "0%";
            }
        }

        //定义触发函数

        //public static void AddTotal1()
        //{          
        //    Total1++;
        //    int i = DateTime.Now.Hour;//小时
        //    TotalArray[i]++;
        //    SaveCount();
        //    if (CountRefreshedevent != null)
        //    {
        //        CountRefreshedevent();
        //    }
        //}
        public static void ShowOKNG(int s, string Result)
        {
            if (OKNGRefreshedevent != null)
            {
                OKNGRefreshedevent(s, Result);
            }
        }
        public static void AddOK1()
        {       
            OK1++;
            Total1++; 
            int i = DateTime.Now.Hour;//小时
            if (Time!=i)
            {
                Time = i;
                OKArray[i]=0;
                TotalArray[i]=0;
                NGArray[i]=0;
            }
            OKArray[i]++;
            TotalArray[i]++;
            SaveCount();
            if (CountRefreshedevent != null)
            {
                CountRefreshedevent();
            }

        }

        public static void AddNG1()
        {      
            NG1++;
            Total1++;
            int i = DateTime.Now.Hour;//小时
            if (Time != i)
            {
                Time = i;
                OKArray[i] = 0;
                TotalArray[i] = 0;
                NGArray[i] = 0;
            }
            NGArray[i]++;
            TotalArray[i]++;
            SaveCount();
            if (CountRefreshedevent != null)
            {
                CountRefreshedevent();
            }
        }

        public static void CountClear()
        {        
            Total1 = 0;
            OK1 = 0;
            NG1 = 0;
            //Array.Clear(TotalArray, 0, TotalArray.Length);//清空数组
            //Array.Clear(OKArray, 0, OKArray.Length);//清空数组
            //Array.Clear(NGArray, 0, NGArray.Length);//清空数组
            SaveCount();
            if (CountRefreshedevent != null)
            {
                CountRefreshedevent();
            }
        }

        public static void SaveCount()
        {
            Tool.IniFile.INIWriteValue(PathData, "数据统计", "总数", Convert.ToString(Total1));
            Tool.IniFile.INIWriteValue(PathData, "数据统计", "OK", Convert.ToString(OK1));
            Tool.IniFile.INIWriteValue(PathData, "数据统计", "NG", Convert.ToString(NG1));
            Tool.IniFile.INIWriteValue(PathData, "数据统计", "Time", Convert.ToString(Time));
            for (int i = 0; i < TotalArray.Length; i++)
            {
                Tool.IniFile.INIWriteValue(PathData, "数据统计", "总数" + i, Convert.ToString(TotalArray[i]));
                Tool.IniFile.INIWriteValue(PathData, "数据统计", "OK" + i, Convert.ToString(OKArray[i]));
                Tool.IniFile.INIWriteValue(PathData, "数据统计", "NG" + i, Convert.ToString(NGArray[i]));
            }

        }

        public static void LoadCount()
        {
            try
            {
                Total1 = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "总数", ""));
                OK1 = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "OK", ""));
                NG1 = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "NG", ""));
                Time = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "Time", ""));
                for (int i = 0; i < TotalArray.Length; i++)
                {
                    TotalArray[i] = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "总数" + i, ""));
                    OKArray[i] = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "OK" + i, ""));
                    NGArray[i] = Convert.ToInt32(Tool.IniFile.INIGetStringValue(PathData, "数据统计", "NG" + i, ""));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "读取生产数据失败");
            }
        }


        #endregion


        #region CT生产统计

        //定义一个委托

        public delegate void CTRefreshedEventHandler();

        //定义一个事件

        public static event CTRefreshedEventHandler CTRefreshedevent;

        public static string time1 = "0";

        public static string time2 = "0";

        //定义触发函数
        public static void AddCT1(string Text)
        {
            time1 = Text;
            if (CTRefreshedevent != null)
            {
                CTRefreshedevent();
            }
        }

        //定义触发函数
        public static void AddCT2(string Text)
        {
            time2 = Text;
            if (CTRefreshedevent != null)
            {
                CTRefreshedevent();
            }
        }

        #endregion
    }
}
